home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / mpegstat / motionve.c < prev    next >
C/C++ Source or Header  |  1997-01-01  |  8KB  |  230 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  * Technical University of Berlin, Germany, Dept. of Computer Science
  4.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  5.  *
  6.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  7.  *
  8.  * This program uses parts of the source code of the Berkeley MPEG player
  9.  *
  10.  * ---------------------------
  11.  *
  12.  * Copyright (c) 1993 Technical University of Berlin, Germany
  13.  *
  14.  * for the parts of the Berkeley player used:
  15.  *
  16.  * Copyright (c) 1992 The Regents of the University of California.
  17.  * All rights reserved.
  18.  *
  19.  * ---------------------------
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software and its
  22.  * documentation for any purpose, without fee, and without written agreement is
  23.  * hereby granted, provided that the above copyright notices and the following
  24.  * two paragraphs appear in all copies of this software.
  25.  * 
  26.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  27.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  28.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  29.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  30.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  * 
  33.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  34.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  35.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  37.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  38.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  39.  * OR MODIFICATIONS.
  40.  */
  41. #include "video.h"
  42. #include "proto.h"
  43. #include "util.h"
  44. #include "dither.h"
  45.  
  46. int max_horizontal_f = 0, max_vertical_f = 0, max_horizontal_b = 0, max_vertical_b = 0;
  47.  
  48. /*
  49.  *--------------------------------------------------------------
  50.  *
  51.  * ComputeVector --
  52.  *
  53.  *    Computes motion vector given parameters previously parsed
  54.  *      and reconstructed.
  55.  *
  56.  * Results:
  57.  *      Reconstructed motion vector info is put into recon_* parameters
  58.  *      passed to this function. Also updated previous motion vector
  59.  *      information.
  60.  *
  61.  * Side effects:
  62.  *      None.
  63.  *
  64.  *--------------------------------------------------------------
  65.  */
  66.  
  67. #define ComputeVector(recon_right_ptr, recon_down_ptr, recon_right_prev, recon_down_prev, f, full_pel_vector, motion_h_code, motion_v_code, motion_h_r, motion_v_r)                \
  68.                                     \
  69. {                                    \
  70.   int comp_h_r, comp_v_r;                        \
  71.   int right_little, right_big, down_little, down_big;            \
  72.   int max, min, new_vector;                        \
  73.                                     \
  74.   /* The following procedure for the reconstruction of motion vectors     \
  75.      is a direct and simple implementation of the instructions given    \
  76.      in the mpeg December 1991 standard draft.                 \
  77.   */                                    \
  78.                                     \
  79.   if (f == 1 || motion_h_code == 0)                    \
  80.     comp_h_r = 0;                            \
  81.   else                                     \
  82.     comp_h_r = f - 1 - motion_h_r;                    \
  83.                                     \
  84.   if (f == 1 || motion_v_code == 0)                    \
  85.     comp_v_r = 0;                            \
  86.   else                                     \
  87.     comp_v_r = f - 1 - motion_v_r;                    \
  88.                                     \
  89.   right_little = motion_h_code * f;                    \
  90.   if (right_little == 0)                        \
  91.     right_big = 0;                            \
  92.   else {                                \
  93.     if (right_little > 0) {                        \
  94.       right_little = right_little - comp_h_r;                \
  95.       right_big = right_little - 32 * f;                \
  96.     }                                    \
  97.     else {                                \
  98.       right_little = right_little + comp_h_r;                \
  99.       right_big = right_little + 32 * f;                \
  100.     }                                    \
  101.   }                                    \
  102.                                     \
  103.   down_little = motion_v_code * f;                    \
  104.   if (down_little == 0)                            \
  105.     down_big = 0;                            \
  106.   else {                                \
  107.     if (down_little > 0) {                        \
  108.       down_little = down_little - comp_v_r;                \
  109.       down_big = down_little - 32 * f;                    \
  110.     }                                    \
  111.     else {                                \
  112.       down_little = down_little + comp_v_r;                \
  113.       down_big = down_little + 32 * f;                    \
  114.     }                                    \
  115.   }                                    \
  116.                                       \
  117.   max = 16 * f - 1;                            \
  118.   min = -16 * f;                            \
  119.                                     \
  120.   new_vector = recon_right_prev + right_little;                \
  121.                                     \
  122.   if (new_vector <= max && new_vector >= min)                \
  123.     *recon_right_ptr = recon_right_prev + right_little;            \
  124.                       /* just new_vector */                \
  125.   else                                    \
  126.     *recon_right_ptr = recon_right_prev + right_big;            \
  127.   recon_right_prev = *recon_right_ptr;                    \
  128.   if (full_pel_vector)                            \
  129.     *recon_right_ptr = *recon_right_ptr << 1;                \
  130.                                     \
  131.   new_vector = recon_down_prev + down_little;                \
  132.   if (new_vector <= max && new_vector >= min)                \
  133.     *recon_down_ptr = recon_down_prev + down_little;            \
  134.                       /* just new_vector */                \
  135.   else                                    \
  136.     *recon_down_ptr = recon_down_prev + down_big;            \
  137.   recon_down_prev = *recon_down_ptr;                    \
  138.   if (full_pel_vector)                            \
  139.     *recon_down_ptr = *recon_down_ptr << 1;                \
  140. }
  141.  
  142. /*
  143.  *--------------------------------------------------------------
  144.  *
  145.  * ComputeForwVector --
  146.  *
  147.  *    Computes forward motion vector by calling ComputeVector
  148.  *      with appropriate parameters.
  149.  *
  150.  * Results:
  151.  *    Reconstructed motion vector placed in recon_right_for_ptr and
  152.  *      recon_down_for_ptr.
  153.  *
  154.  * Side effects:
  155.  *      None.
  156.  *
  157.  *--------------------------------------------------------------
  158.  */
  159.  
  160. void 
  161. ComputeForwVector(recon_right_for_ptr, recon_down_for_ptr)
  162.      int *recon_right_for_ptr;
  163.      int *recon_down_for_ptr;
  164. {
  165.  
  166.   Pict *picture;
  167.   Macroblock *mblock;
  168.  
  169.   picture = &(curVidStream->picture);
  170.   mblock = &(curVidStream->mblock);
  171.  
  172.   ComputeVector(recon_right_for_ptr, recon_down_for_ptr,
  173.         mblock->recon_right_for_prev, 
  174.         mblock->recon_down_for_prev,
  175.         picture->forw_f, picture->full_pel_forw_vector,
  176.         mblock->motion_h_forw_code, mblock->motion_v_forw_code,
  177.         mblock->motion_h_forw_r, mblock->motion_v_forw_r); 
  178.   if (max_horizontal_f < abs(*recon_right_for_ptr))
  179.      max_horizontal_f = abs (*recon_right_for_ptr);
  180.   if (max_vertical_f < abs(*recon_down_for_ptr))
  181.      max_vertical_f = abs (*recon_down_for_ptr);
  182.   mvstat (*recon_right_for_ptr, 0);
  183.   mvstat (*recon_down_for_ptr, 1);
  184. }
  185.  
  186.  
  187. /*
  188.  *--------------------------------------------------------------
  189.  *
  190.  * ComputeBackVector --
  191.  *
  192.  *    Computes backward motion vector by calling ComputeVector
  193.  *      with appropriate parameters.
  194.  *
  195.  * Results:
  196.  *    Reconstructed motion vector placed in recon_right_back_ptr and
  197.  *      recon_down_back_ptr.
  198.  *
  199.  * Side effects:
  200.  *      None.
  201.  *
  202.  *--------------------------------------------------------------
  203.  */
  204.  
  205. void 
  206. ComputeBackVector(recon_right_back_ptr, recon_down_back_ptr)
  207.      int *recon_right_back_ptr;
  208.      int *recon_down_back_ptr;
  209. {
  210.   Pict *picture;
  211.   Macroblock *mblock;
  212.  
  213.   picture = &(curVidStream->picture);
  214.   mblock = &(curVidStream->mblock);
  215.  
  216.   ComputeVector(recon_right_back_ptr, recon_down_back_ptr,
  217.         mblock->recon_right_back_prev, 
  218.         mblock->recon_down_back_prev,
  219.         picture->back_f, picture->full_pel_back_vector,
  220.         mblock->motion_h_back_code, mblock->motion_v_back_code,
  221.         mblock->motion_h_back_r, mblock->motion_v_back_r); 
  222.  
  223.   if (max_horizontal_b < abs(*recon_right_back_ptr))
  224.      max_horizontal_b = abs (*recon_right_back_ptr);
  225.   if (max_vertical_b < abs(*recon_down_back_ptr))
  226.      max_vertical_b = abs (*recon_down_back_ptr);
  227.   mvstat (*recon_right_back_ptr, 2);
  228.   mvstat (*recon_down_back_ptr, 3);
  229. }
  230.